/**
 * TemplateDao.java
 *
 * Copyright (c) 2009 prjmanager All Rights Reserved.
 *
 * This source file may not be copied, modified or redistributed,
 * in whole or in part, in any form or for any reason, without the express
 * written consent of prjmanager .
 **/

package ro.prjmanager.dao;

import org.hibernate.Query;
import ro.prjmanager.core.dao.GenericDAO;
import ro.prjmanager.core.list.ListSqlParser;
import ro.prjmanager.core.list.SQL;
import ro.prjmanager.model.Projects;
import ro.prjmanager.model.Template;

import java.util.List;

/**
 * This interface provides custom method declaration for Template
 * entity and has a default implementation in the same package :
 * TemplateDAOImpl
 * @author Viorel Tacliciu
 * @version 0.0.1
 */
 @SuppressWarnings("unchecked")
public class TemplateDao extends GenericDAO<Template,  java.lang.Integer> {

    @Override
    public List<Template> listPagFilter(int startPage, int resPerPage, String sqlFilter) {
          StringBuilder sql = new StringBuilder("select c from ").append(getPersistentClass().getName()).append(" as c")
                .append(" join c.fkProjectDb as d ");
        SQL sqlFilterParsed = ListSqlParser.parseSqlListParameter(sqlFilter);
        sql.append(" ").append(sqlFilterParsed.getBeforeWhereParent());
        sql.append(" where d.fkProject.id=")
                .append(((Projects)getUserSession().getProject()).getId());
        sql.append(" ").append(sqlFilterParsed.getAfterWhereParent());

        Query sqlQuery = getSession().createQuery(sql.toString());
        
        sqlQuery.setMaxResults(resPerPage);
        sqlQuery.setFirstResult(startPage);

        return sqlQuery.list();
    }


     public List<Template> list() {
        StringBuilder sql = new StringBuilder("select c from ").append(getPersistentClass().getName()).append(" as c");
        sql.append(" join c.fkProjectDb as d ");
        sql.append(" where d.fkProject.id=")
                .append(((Projects)getUserSession().getProject()).getId());
        Query sqlQuery = getSession().createQuery(sql.toString());
        return sqlQuery.list();
    }
}
